package crm.control;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;

import crm.dbcoon.DBConnect;
import crm.model.Week_sum;
import crm.model.all_sum;

public class WeeksumControl {
	/**
	 * 根据
	 * @param id
	 * @return
	 */
	public static all_sum getAllSumByWeekSumId(int id) {
		DBConnect dbc = new DBConnect();
		all_sum allSum = new all_sum();
		String sql = "select all_sum.*from all_sum,week_sum where all_sum.id = week_sum.all_sum_id and week_sum.id = '"+id+"'";
		dbc.prepareStatement(sql);
		ResultSet rs = null;
		try {
			rs = dbc.executeQuery();
			while (rs.next()) {
				allSum.setEmp_id(rs.getInt("emp_id"));
				allSum.setId(rs.getInt("id"));
				allSum.setStart_time(rs.getString("start_time"));
				allSum.setEnd_time(rs.getString("end_time"));
			}
			return allSum;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}finally{
			dbc.close();
		}
	}
	/**
	 * 周总结添加
	 */
	public static void addWeeksum(int all_sum_id,int create_person_id) {
		DBConnect dbc = new DBConnect();
		String sql = "insert into week_sum(all_sum_id,date,content,help_person,description,create_person_id) values(?,?,?,?,?,?)";
		dbc.prepareStatement(sql);
		try {
			int index = 1;
			dbc.setInt(index++, all_sum_id);
			dbc.setString(index++, "");
			dbc.setString(index++, "");
			dbc.setString(index++, "");
			dbc.setString(index++, "");
			dbc.setInt(index++, create_person_id);
			dbc.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			dbc.close();
		}
	}
	
	/**
	 * 周总结删除
	 */
//	public static Boolean delWeeksum(int id) {
//		DBConnect dbc = new DBConnect();
//		String sql = "delete from"
//	}
	/**
	 * 获取周总结列表
	 */
	public static Collection<Week_sum> getAllWeekSums(int allSumId) {
		Collection<Week_sum> ws = new ArrayList<Week_sum>();
		DBConnect dbc = new DBConnect();
		String sql = "select * from week_sum where all_sum_id = '"+allSumId+"'";
		dbc.prepareStatement(sql);
		ResultSet rs = null;
		try {
			rs = dbc.executeQuery();
			while (rs.next()) {
				Week_sum weekSum = new Week_sum();
				weekSum.setId(rs.getInt("id"));
				weekSum.setAll_sum_id(rs.getInt("all_sum_id"));
				weekSum.setDate(rs.getString("date"));
				weekSum.setContent(rs.getString("content"));
				weekSum.setHelp_person(rs.getString("help_person"));
				weekSum.setDescription(rs.getString("description"));
				weekSum.setCreate_person_id(rs.getInt("create_person_id"));
				ws.add(weekSum);
			}
			return ws;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}finally{
			dbc.close();
		}
	}
	/**
	 * 
	 * @param id
	 * @return
	 */
	public static Week_sum getWeekSumByid(int id) {
		DBConnect dbc = new DBConnect();
		Week_sum weekSum = new Week_sum();
		String sql = "select * from week_sum where id='"+id+"'";
		dbc.prepareStatement(sql);
		ResultSet rs = null;
		try {
			rs=(ResultSet)dbc.executeQuery();
			if(rs.next()){
				weekSum.setId(rs.getInt("id"));
				weekSum.setAll_sum_id(rs.getInt("all_sum_id"));
				weekSum.setDate(rs.getString("date"));
				weekSum.setContent(rs.getString("content"));
				weekSum.setDescription(rs.getString("description"));
				weekSum.setCreate_person_id(rs.getInt("create_person_id"));
			}
			return weekSum;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("get出错");
			return null;
		}finally{
			dbc.close();
		}
	}
	/**
	 * 
	 * @param weekSum
	 * @param id
	 * @return
	 */
	public static Boolean updateWeekSum(Week_sum weekSum,int id) {
		DBConnect dbc = new DBConnect();
		String sql = "update week_sum set date=?,content=?,help_person=?,description=? where id='"+id+"'";
		dbc.prepareStatement(sql);
		try {
			int index = 1;
			dbc.setString(index++, weekSum.getDate());
			dbc.setString(index++, weekSum.getContent());
			dbc.setString(index++, weekSum.getHelp_person());
			dbc.setString(index++, weekSum.getDescription());
			dbc.executeUpdate();
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
	}

}
